package com.chen.im.view;

import com.chen.boot.db.entity.ContactsInfo;
import com.chen.boot.service.ChatMessageService;
import com.chen.common.SpringUtil;
import com.chen.im.MessageType;

import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.net.URL;
import java.util.Optional;
import java.util.ResourceBundle;

import de.felixroske.jfxsupport.FXMLController;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.MultipleSelectionModel;
import javafx.scene.control.TextArea;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

/**
 * @Description
 * @Author Pengwei
 * @CreatDate 2021/11/14 13:48
 */
@Slf4j
@Getter
@FXMLController
public class ChatEdit implements Initializable {
    public static ChatEdit instance;

    @FXML
    private TextArea editText;  //消息编辑框

    @FXML
    private Button sendBtn;

    @FXML
    public void sendMsg() {
        //发送文本消息
        sendTextMessage();

        editText.setText("");
    }

    //发送文本消息
    public void sendTextMessage() {
        if (StringUtils.isBlank(editText.getText())) {
            return;
        }
        MultipleSelectionModel<ContactsInfo> selectionModel = ContactsList.instance.getContactsListView().getSelectionModel();
        ContactsInfo contactsInfo = Optional.ofNullable(selectionModel).map(v -> v.getSelectedItem()).orElse(null);
        if (contactsInfo == null) {
            return;
        }
        //发送消息
        ChatMessageService chatMessageService = SpringUtil.getBean(ChatMessageService.class);
        chatMessageService.sendMessage(contactsInfo,editText.getText(), MessageType.text );
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        instance = this;
    }
}
